//
//  AdViewController.m
//  SXH
//
//  Created by 龚啸 on 15/9/23.
//  Copyright © 2015年 lyss. All rights reserved.
//

#import "AdViewController.h"
#import "UIImageView+WebCache.h"
#import "CommonService.h"
#import "GXWebConViewController.h"
#import "WrapperMainViewController.h"
#import "AppDelegate.h"

#import "MainTbaBarController.h"
@interface AdViewController ()

@property(nonatomic,copy) NSString *url;
@property(nonatomic,strong) NSTimer *myTimer;
@property(nonatomic,strong) UIImageView *myImageView;
@property(nonatomic,strong) UIImageView *imageLogo;
@property (nonatomic, strong) UIButton *countdownBtn;
@property (nonatomic, assign) int count;
@end

@implementation AdViewController
@synthesize myImageView,url,img_url,imageLogo;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self getImgData];
//    if (iPhone4s) {
//        imageLogo =[[UIImageView alloc]initWithFrame:CGRectMake(0, -88, 320, 568)];
//        myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,371)];
//    }else{
        myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,ScreenHeight*464/568)];
        imageLogo =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
//    }
    
    myImageView.userInteractionEnabled = YES;
    self.view.backgroundColor = [UIColor whiteColor];

    [imageLogo setImage:[UIImage imageNamed:@"default_logo@2x"]];
    [self.view addSubview:imageLogo];
    [self.view addSubview:myImageView];
    _count = 3;
    _myTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
    [[NSRunLoop  currentRunLoop] addTimer:_myTimer forMode:NSDefaultRunLoopMode];
    [self.navigationController setNavigationBarHidden: YES];
    
    self.myImageView.alpha = 0;
    [self initCountDownBtn];
}
/**
 *  广告倒计时
 */
- (void)initCountDownBtn {
    _countdownBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _countdownBtn.frame = CGRectMake(SCREEN_WIDTH-80, 40, 60, 25);
    _countdownBtn.layer.cornerRadius = 12.5;
    _countdownBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:2];
    _countdownBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    [UIView commitAnimations];
    [_countdownBtn addTarget:self action:@selector(skip) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_countdownBtn];
}

- (void)getImgData{
    CommonService *service = [[CommonService alloc] init];
    NSDictionary *param = nil;
    [service getNetWorkData:param
                       Path:ADIMAGE
                  Successed:^(id entity) {
                      NSDictionary *dic = (NSDictionary *)entity;
                      url = [dic valueForKeyPath:@"url"];
                      if (iPhone4s) {
                          img_url = [dic valueForKeyPath:@"s4_img_url"];
                      }else{
                          img_url = [dic valueForKeyPath:@"img_url"];
                      }
                      
                      if (img_url) {
                          NSString *imgUrl =[NSString stringWithFormat:@"%@/%@",IMAGE_URL,img_url];
                          [myImageView sd_setImageWithURL:[NSURL URLWithString:imgUrl]
                      completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
                          {
                              [UIView beginAnimations:nil context:nil];
                              [UIView setAnimationDuration:1];
                              myImageView.alpha  = 1;
                              [UIView commitAnimations];
                          }];
                       }
                         
                     
                      if (url && ![url isEqualToString: @""]) {
                          UITapGestureRecognizer *click = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(show)];
                          
                          [myImageView addGestureRecognizer:click];
                      }
                      dispatch_async(dispatch_get_main_queue(), ^{
                      });
                  } Failed:^(NSInteger errorCode, NSString *message) {
                      NSLog(@"===");
                  }];
}

-(void)show
{
    GXWebConViewController *vc = [[GXWebConViewController alloc] init];
     vc.url =url;
    [_myTimer invalidate];
    [self.navigationController pushViewController:vc animated:YES];

    DebugLog(@"ok");
}

- (void)skip {
    _count = -1;
    [self timerFired];
}

-(void)timerFired
{
    DebugLog(@"ok");
    if (_count >= 0) {
        [_countdownBtn setTitle:[NSString stringWithFormat:@"%d 跳过",_count] forState:UIControlStateNormal];
        [_countdownBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _countdownBtn.titleLabel.font = [UIFont fontWithName:@"DBLCDTempBlack" size:13];
        --_count;
    }else{
        //      WrapperMainViewController *vc = [[WrapperMainViewController alloc] init];
        MainTbaBarController *vc =[[MainTbaBarController alloc]init];
        
        [[AppDelegate instance].window changeRootViewController:vc];
        [_myTimer invalidate];
        //    UIWindow *window = [UIApplication sharedApplication].keyWindow;
        //    UINavigationController * nav = [[UINavigationController alloc ]initWithRootViewController:[[WrapperMainViewController alloc] init]];
        //    window.rootViewController = nav;
    }

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"AdViewController"];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"AdViewController"];
}

@end
